<?php

/**
 * Texy! - web text markup-language
 * --------------------------------
 *
 * Copyright (c) 2004, 2008 David Grudl aka -dgx- (http://www.dgx.cz)
 *
 * This source file is subject to the GNU GPL license that is bundled
 * with this package in the file license.txt.
 *
 * For more information please see http://texy.info/
 *
 * @copyright  Copyright (c) 2004, 2008 David Grudl
 * @license    GNU GENERAL PUBLIC LICENSE version 2 or 3
 * @link       http://texy.info/
 * @package    Texy
 */



/**
 * Scripts module.
 *
 * @author     David Grudl
 * @copyright  Copyright (c) 2004, 2008 David Grudl
 * @package    Texy
 * @version    $Revision: 201 $ $Date: 2008-02-01 02:00:29 +0000 (Fri, 01 Feb 2008) $
 */
final class TexyScriptModule extends TexyModule
{
    /**
     * @var callback|object  script elements handler
     * function myFunc($parser, $cmd, $args, $raw)
     */
    public $handler;


    /** @var string  arguments separator */
    public $separator = ',';



    public function __construct($texy)
    {
        $this->texy = $texy;

        $texy->addHandler('script', array($this, 'solve'));

        $texy->registerLinePattern(
            array($this, 'pattern'),
            '#\{\{([^'.TEXY_MARK.']+)\}\}()#U',
            'script'
        );
    }



    /**
     * Callback for: {{...}}.
     *
     * @param  TexyLineParser
     * @param  array      regexp matches
     * @param  string     pattern name
     * @return TexyHtml|string|FALSE
     */
    public function pattern($parser, $matches)
    {
        list(, $mContent) = $matches;
        //    [1] => ...

        $cmd = trim($mContent);
        if ($cmd === '') return FALSE;

        $args = $raw = NULL;
        // function(arg, arg, ...)  or  function: arg, arg
        if (preg_match('#^([a-z_][a-z0-9_-]*)\s*(?:\(([^()]*)\)|:(.*))$#iu', $cmd, $matches)) {
            $cmd = $matches[1];
            $raw = isset($matches[3]) ? trim($matches[3]) : trim($matches[2]);
            if ($raw === '')
                $args = array();
            else
                $args = preg_split('#\s*' . preg_quote($this->separator, '#') . '\s*#u', $raw);
        }

        // Texy 1.x way
        if ($this->handler) {
            if (is_callable(array($this->handler, $cmd))) {
                array_unshift($args, $parser);
                return call_user_func_array(array($this->handler, $cmd), $args);
            }

            if (is_callable($this->handler))
                return call_user_func_array($this->handler, array($parser, $cmd, $args, $raw));
        }

        // Texy 2 way
        return $this->texy->invokeAroundHandlers('script', $parser, array($cmd, $args, $raw));
    }



    /**
     * Finish invocation.
     *
     * @param  TexyHandlerInvocation  handler invocation
     * @param  string  command
     * @param  array   arguments
     * @param  string  arguments in raw format
     * @return TexyHtml|string|FALSE
     */
    public function solve($invocation, $cmd, $args, $raw)
    {
        if ($cmd === 'texy') {
            if (!$args) return FALSE;

            switch ($args[0]) {
            case 'nofollow':
                $this->texy->linkModule->forceNoFollow = TRUE;
                break;
            }
            return '';

        } else {
            return FALSE;
        }
    }

}
